package javacommon.util;

/**
 * author:sfk
 * 完成日常日期转换
 */

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class DateUtil {

	public static final String FULL_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_PATTERN = "yyyy-MM-dd";
	public static final String MONTH_PATTERN = "yyyy-MM";
	public static final String NOT_SEPARATOR_DATE_PATTERN = "yyyyMMdd"; 
	static java.text.SimpleDateFormat sdfShort = new java.text.SimpleDateFormat("yyyyMMdd");
	public static String dateFormat(Date date) {
		String result = null;
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		if (date == null) {
			return "";
		} else {
			result = sd.format(date);
		}
		return result;
	}

	/**
	 * 根据格式获得日期字符串
	 * 
	 * @author bzq add
	 * @param sFormat
	 * @return
	 */
	public static String getDateStr(String sFormat) {
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
		String nowTime = sdf.format(now);
		return (nowTime);
	}

	public static Date getDate(String date) {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date result = null;
		try {
			result = sd.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * 字符串转成指定格式日期
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static Date getDate(String date, String format) {
		SimpleDateFormat sd = new SimpleDateFormat(format);
		Date result = null;
		try {
			result = sd.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return result;
	}

	public static String getDate() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String mDateTime = formatter.format(cal.getTime());
		return mDateTime;
	}

	/**
	 * 取得有时分秒的日期(Timestamp)
	 * 
	 * @author pch 2010.7.8
	 * 
	 * @return Timestamp
	 */
	public static Timestamp getTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * 将Timestamp转化为字符串(yyyy/MM/dd HH:mm:ss)
	 * 
	 * @author pch 2010.7.8
	 * 
	 * @return 日期字符串
	 */
	public static String parseTimestampToStr(Timestamp time) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		formatter.format(time);
		return formatter.format(time);
	}

	/**
	 * 时间前推或后推分钟,其中JJ表示分钟.
	 */
	public static String getPreTime(String sj1, String jj) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String mydate1 = "";
		try {
			Date date1 = format.parse(sj1);
			long Time = (date1.getTime() / 1000) - Integer.parseInt(jj) * 60;
			date1.setTime(Time * 1000);
			mydate1 = format.format(date1);
		} catch (Exception e) {
		}
		return mydate1;
	}

	/**
	 * 返回当前时间 两位年份+两位月份+两位日期+两位小时+两位分钟+两位秒 的字符串
	 * 
	 * @return
	 */
	public static String getTimeStr() {
		Date date = new java.util.Date();
		SimpleDateFormat sd = new SimpleDateFormat("yyMMddHHmmss");
		String dateStr = sd.format(date);
		return dateStr;
	}

	public static void main(String[] args) {
		String now = getDateBefore(7);
		System.out.println(now);
	}

	// ////kingter.jar

	public static String dateToString(String pattern, Date date) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	public static String currenDateToString() {
		return dateToString("yyyy-MM-dd HH:mm:ss", new Date());
	}

	public static String currenDateToString(String pattern) {
		return dateToString(pattern, new Date());
	}

	public static boolean compareDate(String pattern, String sFirstDate,
			String sSecondlyDate) {
		Date firstDate = formatDate(pattern, sFirstDate);
		Date secondlyDate = formatDate(pattern, sSecondlyDate);
		return firstDate.before(secondlyDate);
	}

	public static boolean afterDate(String pattern, String sFirstDate,
			String sSecondlyDate) {
		Date firstDate = formatDate(pattern, sFirstDate);
		Date secondlyDate = formatDate(pattern, sSecondlyDate);
		return firstDate.after(secondlyDate);
	}

	public static Date formatDate(String pattern, String sDate) {
		Date date = null;
		DateFormat dateFormat = new SimpleDateFormat(pattern);
		try {
			date = dateFormat.parse(sDate);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return date;
	}

	public static Date rollDate(Date date, int field, int step) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(field, step);
		return calendar.getTime();
	}

	public static Date setDate(Date date, int field, int value) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(field, value);
		return calendar.getTime();
	}

	public static int getField(int field) {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(field);
	}

	public static int getField(Date date, int field) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(field);
	}

	/**
	 * 得到几天后的时间
	 * 
	 * @param d
	 * @param day
	 * @return
	 */
	public static Date getDateAfter(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}

	/**
	 * 得到几天前的时间
	 * 
	 * @param d
	 * @param day
	 * @return
	 */
	public static Date getDateBefore(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return now.getTime();
	}

	/**
	 * 得到几天前或后的时间 +d 后 -d 前
	 * 
	 * @param d
	 * @param day
	 * @return
	 */
	public static String getDateBefore(int day) {
		Date date = new java.util.Date();
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String mDateTime = formatter.format(now.getTime());
		return mDateTime;

	}

	/**
	 * 用当前时间+N位随机数
	 * 
	 * @return
	 */
	public static String generateFileName() {
		// 获得当前时间
		DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		// 转换为字符串
		String formatDate = format.format(new Date());
		// 随机生成文件编号
		int random = new Random().nextInt(10000);
		return new StringBuffer().append(formatDate).append(random).toString();
	}

	/**
	 * Descrption:取得当前日期,格式为:YYYYMMDD
	 * 
	 * @return String 11
	 * @throws java.lang.Exception
	 */
	public static String getNowShortDate() throws Exception {
		String nowDate = "";
		try {
			java.sql.Date date = null;
			date = new java.sql.Date(new java.util.Date().getTime());
			nowDate = sdfShort.format(date);
			return nowDate;
		} catch (Exception e) {
			throw e;
		}
	}

}
